#!/bin/bash
# Author： Syuan-Rong Chen
# E-mail： calvin.atx@gmail.com
# Blog：   http://calvinit.blogspot.com
# Date : 20100815
#
# <description>
# Copyright (C) <year>  <name> <email>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see .
#
drvm=$HOME/calvin/cvirt/virt_net.txt
vmnum=$(wc $HOME/calvin/cvirt/virt_net.txt|fmt -u|cut -d' ' -f3)

clear

function ck_vmtp {
	tp=1
	s=$(cat $drvm)
	for us in $s	
	do
		t1[tp]=${us%%:*}
		t2[tp]=${us##*:}
		virsh net-list --all > /tmp/ctmp1
		cat /tmp/ctmp1 | grep ${t2[tp]} > /tmp/ctmp2
		[ $? == "0" ] && text="網路存在" || text="無此網路"
		echo -e [$tp] [$text]"\t"${t2[tp]}
		tp=$(( $tp + 1 ))
	done
}

function ck_vmast {
	s=$(cat $drvm)

	for us in $s
	do
		u1=${us%%:*}
		u2=${us##*:}
		
		virsh net-destroy $u2
		virsh net-undefine $u2
		echo -e '\E[1;31m' 網路:$u2 已從清單中刪除 ; tput sgr0
	done
}

function outputMenu {
echo -e '\E[1;33m'"歡迎使用Calvin_Virt Manager"; tput sgr0
echo ""
echo -e '\E[1;32m'"請選擇要刪除的虛擬機器"; tput sgr0
echo ""
echo -e [id] 狀態"\t"網路名稱
echo "---------------------------"
ck_vmtp
echo ""
echo "[a] 全部刪除"
echo "[0] 回上一層"

echo ""
echo -ne "請選擇: "
read USERCHOICE
clear

case $USERCHOICE in
	"a") ck_vmast ;;
	"0") . /usr/cbin/d-cvirt/net.sh ;;
        $USERCHOICE) 
		virsh net-destroy ${t2[$USERCHOICE]}
		virsh net-undefine ${t2[$USERCHOICE]} 
		echo -e '\E[1;31m' 網路:${t2[$USERCHOICE]} 已從清單中刪除 ; tput sgr0
		;;
        *) echo "請輸入正確的選擇";;
esac
}

. /usr/cbin/c/domenu.sh
